# Python3_GF_Python-Docx_Specific_Functions.py
# Create By GF 2023-12-19 14:28

# ----------------------------------------------------------------------------------------------------

# Docx GF Extract Content(Cry) Row By One Keyword.
def Docx_Extract_Cry_Row_By_One_Keyword(Docx_Path:str, Keyword:str):

    """[Required] import docx [Version 0.7.4]"""
    # ----------------------------------------------
    ObjDocx = docx.Document(Docx_Path)
    # ----------------------------------------------
    for paragraph in ObjDocx.paragraphs:
        if (Keyword in paragraph.text):
            return paragraph.text
    # ----------------------------------------------
    # End of Function.

# Docx GF Extract Content(Cry) Row By Two Keyword.
def Docx_Extract_Cry_Row_By_Two_Keyword(Docx_Path:str, Keyword_1:str, Keyword_2:str):

    """[Required] import docx [Version 0.7.4]"""
    # ----------------------------------------------
    ObjDocx = docx.Document(Docx_Path)
    # ----------------------------------------------
    for paragraph in ObjDocx.paragraphs:
        if (Keyword_1 in paragraph.text) and (Keyword_2 in paragraph.text):
            return paragraph.text
    # ----------------------------------------------
    # End of Function.

# Docx GF Extract Content(Cry) Title.
def Docx_Extract_Cry_Title(Docx_Path:str):

    """[Required] import docx [Version 0.7.4]"""
    # ----------------------------------------------
    ObjDocx = docx.Document(Docx_Path)
    # ----------------------------------------------
    for index, paragraph in enumerate(ObjDocx.paragraphs):
        #print(index, paragraph.text)
        # ------------------------------------------
        Judgement:int = 0
        # ------------------------------------------
        if (paragraph.alignment == docx.enum.text.WD_PARAGRAPH_ALIGNMENT.CENTER): Judgement += 1
        # ------------------------------------------
        if (len(paragraph.text) > 0): Judgement += 1
        # ------------------------------------------
        if (Judgement == 2): return paragraph.text
    # ----------------------------------------------
    # End of Function.

# Docx GF Extract Table All.
def Docx_Extract_Table_All(Docx_Path:str, Table_No:int):

    """[Required] import docx [Version 0.7.4]"""
    # ----------------------------------------------
    ObjDocx = docx.Document(Docx_Path)
    # ----------------------------------------------
    if (Table_No >= len(ObjDocx.tables)): # -> Table number exceeds index.
        # ------------------------------------------
        return None
    # ----------------------------------------------
    Table_Rows_Num = len(ObjDocx.tables[Table_No].rows)
    Table_Cols_Num = len(ObjDocx.tables[Table_No].columns)
    # ----------------------------------------------
    Table_All:list = []
    Table_Row:list = []
    # ----------------------------------------------
    for RowIdx in range(0, Table_Rows_Num): # -> Extract table data.
        for ColIdx in range(0, Table_Cols_Num):
            try:
                Table_Row.append(ObjDocx.tables[Table_No].cell(RowIdx, ColIdx).paragraphs[0].text)
            except IndexError: # -> # IndexError: cell index [x] is out of range.
                Table_Row.append("Null")
        # ------------------------------------------
        Table_All.append(Table_Row.copy())
        Table_Row.clear()
    # ----------------------------------------------
    return Table_All

# Docx GF Extract Table Col By Column Name.
def Docx_Extract_Table_Col_By_ColName(Docx_Path:str, Table_No:int, ColName:str) -> list:

    """[Required] import docx [Version 0.7.4]"""
    # ----------------------------------------------
    Table_All:list = Docx_Extract_Table_All(Docx_Path, Table_No)
    # ----------------------------------------------
    if (Table_All is None): return list([]) # -> Return empty list.
    # ----------------------------------------------
    Table_All_Transpose:list = []
    Table_Col:list = []
    # ----------------------------------------------
    for ColIdx in range(0, len(Table_All[0])): # -> Row and column transposition.
        for RowIdx in range(0, len(Table_All)):
            Table_Col.append(Table_All[RowIdx][ColIdx])
        # ------------------------------------------
        Table_All_Transpose.append(Table_Col.copy())
        Table_Col.clear()
    # ----------------------------------------------
    for Col in Table_All_Transpose:
        if (ColName in Col): return Col
    # ----------------------------------------------
    return list([]) # -> Return empty list.

# Docx GF Review Field Stored By Row or Column.
def Docx_Review_Field_Stored_By_Row_Or_Column(Docx_Path:str, Table_No:int, FldName:str, FldCryHint:str) -> str:

    """[Required] import docx [Version 0.7.4]"""
    # ----------------------------------------------
    Table_All:list = Docx_Extract_Table_All(Docx_Path, Table_No)
    # ----------------------------------------------
    TableRowsNum:int = None
    TableColsNum:int = None
    # ----------------------------------------------
    if (Table_All == None):
        return "Empty" # -> If Table is Empty.
    else:
        TableRowsNum = len(Table_All)
        TableColsNum = len(Table_All[0])
    # ----------------------------------------------
    
    # Condition Judgement - Start.
    if   (TableRowsNum == 1):
        
        return "Stored By Horizontal"
    
    elif (TableRowsNum == 2) and (TableColsNum == 1):
    
        for X in range(0, len(Table_All[0])):    # -> Finding in Table 1st Row.
            if (FldName in Table_All[0][X]) and (FldCryHint in Table_All[1][X]): return "Stored By Vertical"
    
    elif (TableRowsNum == 2) and (TableColsNum >= 2):
        
        for X in range(0, len(Table_All[0])):    # -> Finding in Table 1st Row.
            if (FldName in Table_All[0][X]) and (FldCryHint in Table_All[1][X]): return "Stored By Vertical"
        # ------------------------------------------
        for Y in range(0, len(Table_All)):       # -> Finding in Table 1st Col.
            if (FldName in Table_All[Y][0]) and (FldCryHint in Table_All[Y][1]): return "Stored By Horizontal"
    
    elif (TableRowsNum >= 3) and (TableColsNum == 1):
    
        for X in range(0, len(Table_All[0])):    # -> Finding in Table 1st Row.
            if (FldName in Table_All[0][X]) and (FldCryHint in Table_All[1][X]): return "Stored By Vertical"
        # ------------------------------------------
        for X in range(0, len(Table_All[0])):    # -> Finding in Table 1st Row, Hint for Cross Row.
            if (FldName in Table_All[0][X]) and (FldCryHint in Table_All[2][X]): return "Stored By Vertical"
        # ------------------------------------------
        for X in range(0, len(Table_All[1])):    # -> Finding in Table 2th Row.
            if (FldName in Table_All[1][X]) and (FldCryHint in Table_All[2][X]): return "Stored By Vertical"
    
    elif (TableRowsNum >= 3) and (TableColsNum >= 2):
    
        for X in range(0, len(Table_All[0])):    # -> Finding in Table 1st Row.
            if (FldName in Table_All[0][X]) and (FldCryHint in Table_All[1][X]): return "Stored By Vertical"
        # ------------------------------------------
        for X in range(0, len(Table_All[0])):    # -> Finding in Table 1st Row, Hint for Cross Row.
            if (FldName in Table_All[0][X]) and (FldCryHint in Table_All[2][X]): return "Stored By Vertical"
        # ------------------------------------------
        for X in range(0, len(Table_All[1])):    # -> Finding in Table 2th Row.
            if (FldName in Table_All[1][X]) and (FldCryHint in Table_All[2][X]): return "Stored By Vertical"
        # ------------------------------------------
        for Y in range(0, len(Table_All)):       # -> Finding in Table 1st Col.
            if (FldName in Table_All[Y][0]) and (FldCryHint in Table_All[Y][1]): return "Stored By Horizontal"
    # Condition Judgement - Ends.

    # ----------------------------------------------
    return "Error"

# Docx GF Extract Table Series By Field Name.
def Docx_Extract_Table_Series_By_FldName(Docx_Path:str, Table_No:int, FldName:str, FldCryHint:str) -> list:

    """[Required] import docx [Version 0.7.4]"""
    # ----------------------------------------------
    Table_All:list = Docx_Extract_Table_All(Docx_Path, Table_No)
    # ----------------------------------------------
    if (Table_All is None): return list([]) # -> Return empty list.
    # ----------------------------------------------
    Field_Stored_By_Row_Or_Column:str = Docx_Review_Field_Stored_By_Row_Or_Column(Docx_Path, Table_No, FldName, FldCryHint)
    # ----------------------------------------------

    # Condition Judgement - Start.
    if   (Field_Stored_By_Row_Or_Column == "Stored By Horizontal"):
        
        for Row in Table_All:
            if (FldName in Row):
                return Row
    
    elif (Field_Stored_By_Row_Or_Column == "Stored By Vertical"):

        Table_All_Transpose:list = []
        Table_Col:list = []
        # ------------------------------------------
        for ColIdx in range(0, len(Table_All[0])): # -> Row and column transposition.
            for RowIdx in range(0, len(Table_All)):
                Table_Col.append(Table_All[RowIdx][ColIdx])
            # --------------------------------------
            Table_All_Transpose.append(Table_Col.copy())
            Table_Col.clear()
        # ------------------------------------------
        for Col in Table_All_Transpose:
            if (FldName in Col):
                return Col
    # Condition Judgement - Ends.

    # ----------------------------------------------
    return list([]) # -> Return empty list.

# ----------------------------------------------------------------------------------------------------
# EOF
